

 #include <QtGui>
 #include <QFileDialog>

 #include "glwidget.h"
 #include "mainwindow.h"
 #include "viewerwidget.h"
 #include "animatorwidget.h"
 #include "rawdatawidget.h"
 
 #include "robot.h"
 #include "core.h"

MainWindow::MainWindow(QApplication *app, QStringList *arguments)
{
	parent = app;
	
	initialize();
	
	setWindowTitle(programName);
	
	setCentralWidget(tabs);

}

MainWindow::~MainWindow()
{
	

}

void MainWindow::initialize()
{
	programName = QString(tr("UBC Robot Visualizer"));

	robot = new Robot;
	core = new Core(this, robot);
	
 //    core->loadScaraRobot();  //for testing only
        core->loadRobotFromFile("Stanford.rbt");


	viewer = new viewerWidget(robot, core);
	animator = new animatorWidget(robot, core);
	rawdata = new rawdataWidget(robot, core);
	tabs = new QTabWidget;
	
	connect(core, SIGNAL(robotLoaded()), viewer, SLOT(robotLoaded()));
	connect(core, SIGNAL(robotLoaded()), rawdata, SLOT(robotLoaded()));
	connect(core, SIGNAL(robotLoaded()), animator, SLOT(robotLoaded()));
	
	viewerIndex = tabs->addTab(viewer,"Viewer");
	animatorIndex = tabs->addTab(animator, "Animator");
	rawdataIndex = tabs->addTab(rawdata, "Raw Data");
	
	animator->setTabIndex(animatorIndex);
	rawdata->setTabIndex(rawdataIndex);
	
	connect(tabs, SIGNAL(currentChanged(int)), animator, SLOT(tabSelected(int)));
	connect(tabs, SIGNAL(currentChanged(int)), rawdata, SLOT(tabSelected(int)));
	
	tabs->setCurrentIndex(viewerIndex);
	
	createActions();
	createToolBars();
	createMenus();
	createStatusBar();
	
	robotLoaded();
}

void MainWindow::createActions()
{
	openAction = new QAction(tr("&Open"), this);
	connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
	
	saveAction = new QAction(tr("&Save"), this);
	connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
	
// 	showAxesAction = new QAction(tr("Show &Axes"), this);
// 	connect(showAxesAction, SIGNAL(triggered()), this, SLOT(showAxes()));
	
	showAllJointDataAction = new QAction(tr("&Display All Joint Data"), this);
	connect(showAllJointDataAction, SIGNAL(triggered()), this, SLOT(showAllJointData()));
	
	exportRobotDataAction = new QAction(tr("&Export Robot Data"), this);
	connect(exportRobotDataAction, SIGNAL(triggered()), this, SLOT(exportRobotData()));
	
	exitAction = new QAction(tr("&Exit"), this);
	connect(exitAction, SIGNAL(triggered()), parent, SLOT(quit()));

	aboutAction = new QAction(tr("&About"), this);
	connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
	
	resetViewAction = new QAction(tr("&Reset Robot Zoom / Pan"), this);
	connect(resetViewAction, SIGNAL(triggered()), viewer, SLOT(resetView()));
	
	//done like this instead of using a QList because I'm too lazy to derive a new class with a jointNumber
	//property from QAction (which would allow me to have one slot to connect to)
	
	drawAxesJoint0Act = new QAction(tr("Draw Axes Joint &0"), this);
	drawAxesJoint0Act->setCheckable(true);
	connect(drawAxesJoint0Act, SIGNAL(toggled(bool)), this, SLOT(drawAxesJoint0(bool)));
	
	drawAxesJoint1Act = new QAction(tr("Draw Axes Joint &1"), this);
	drawAxesJoint1Act->setCheckable(true);
	connect(drawAxesJoint1Act, SIGNAL(toggled(bool)), this, SLOT(drawAxesJoint1(bool)));
	
	drawAxesJoint2Act = new QAction(tr("Draw Axes Joint &2"), this);
	drawAxesJoint2Act->setCheckable(true);
	connect(drawAxesJoint2Act, SIGNAL(toggled(bool)), this, SLOT(drawAxesJoint2(bool)));
	
	drawAxesJoint3Act = new QAction(tr("Draw Axes Joint &3"), this);
	drawAxesJoint3Act->setCheckable(true);
	connect(drawAxesJoint3Act, SIGNAL(toggled(bool)), this, SLOT(drawAxesJoint3(bool)));
	
	drawAxesJoint4Act = new QAction(tr("Draw Axes Joint &4"), this);
	drawAxesJoint4Act->setCheckable(true);
	connect(drawAxesJoint4Act, SIGNAL(toggled(bool)), this, SLOT(drawAxesJoint4(bool)));
	
	drawAxesJoint5Act = new QAction(tr("Draw Axes Joint &5"), this);
	drawAxesJoint5Act->setCheckable(true);
	connect(drawAxesJoint5Act, SIGNAL(toggled(bool)), this, SLOT(drawAxesJoint5(bool)));
	
	drawAxesJoint6Act = new QAction(tr("Draw Axes Joint &6"), this);
	drawAxesJoint6Act->setCheckable(true);
	connect(drawAxesJoint6Act, SIGNAL(toggled(bool)), this, SLOT(drawAxesJoint6(bool)));
	
	drawAllAxesAction = new QAction(tr("Draw &All Axes"), this);
	connect(drawAllAxesAction, SIGNAL(triggered()), this, SLOT(showAllAxes()));
	
	drawNoAxesAction = new QAction(tr("Draw &No Axes"), this);
	connect(drawNoAxesAction, SIGNAL(triggered()), this, SLOT(showNoAxes()));
	
	
}


void MainWindow::createMenus()
{
	fileMenu = menuBar()->addMenu(tr("&Robot"));
	fileMenu->addAction(openAction);
	fileMenu->addAction(exportRobotDataAction);
	fileMenu->addSeparator();
	fileMenu->addAction(exitAction);
	
	drawAxesMenu = new QMenu(tr("Draw &Axes"));
	drawAxesMenu->addAction(drawNoAxesAction);
	drawAxesMenu->addAction(drawAllAxesAction);
	drawAxesMenu->addAction(drawAxesJoint0Act);
	drawAxesMenu->addAction(drawAxesJoint1Act);
	drawAxesMenu->addAction(drawAxesJoint2Act);
	drawAxesMenu->addAction(drawAxesJoint3Act);
	drawAxesMenu->addAction(drawAxesJoint3Act);
	drawAxesMenu->addAction(drawAxesJoint4Act);
	drawAxesMenu->addAction(drawAxesJoint5Act);
	drawAxesMenu->addAction(drawAxesJoint6Act);
	
	
	robotMenu = menuBar()->addMenu(tr("&View"));
	robotMenu->addMenu(drawAxesMenu);
	robotMenu->addAction(showAllJointDataAction);
	robotMenu->addAction(resetViewAction);
	
	helpMenu = menuBar()->addMenu(tr("&Help"));
	helpMenu->addAction(aboutAction);
}

void MainWindow::robotLoaded()	//SLOT
{
	//go through the robot and disable axes controls for joints that don't exist
	showAllAxes();
	showNoAxes();
}

void MainWindow::createToolBars()
{

	

}


void MainWindow::createStatusBar()
{
	statusBar()->showMessage(tr("Ready"));

}


bool MainWindow::open()
{
	QString filename;
	
	filename = QFileDialog::getOpenFileName(this, tr("Open Robot File"), ".", tr("Robot files (*.rbt *.robot)"));
	
	core->loadRobotFromFile(filename);
}

bool MainWindow::save()
{


}

void MainWindow::showAllJointData()
{
	tabs->setCurrentIndex(rawdataIndex);
}

void MainWindow::exportRobotData()
{
	core->exportData(15);
}

void MainWindow::showAllAxes()
{
	drawAxesJoint0(true);
	drawAxesJoint1(true);
	drawAxesJoint2(true);
	drawAxesJoint3(true);
	drawAxesJoint4(true);
	drawAxesJoint5(true);
	drawAxesJoint6(true);
	
	switch (robot->jointList.size())
	{
	case 0:
		drawAxesJoint0Act->setEnabled(false);
		drawAxesJoint1Act->setEnabled(false);
		drawAxesJoint2Act->setEnabled(false);
		drawAxesJoint3Act->setEnabled(false);
		drawAxesJoint4Act->setEnabled(false);
		drawAxesJoint5Act->setEnabled(false);
		drawAxesJoint6Act->setEnabled(false);
		
		drawAxesJoint0Act->setChecked(false);
		drawAxesJoint1Act->setChecked(false);
		drawAxesJoint2Act->setChecked(false);
		drawAxesJoint3Act->setChecked(false);
		drawAxesJoint4Act->setChecked(false);
		drawAxesJoint5Act->setChecked(false);
		drawAxesJoint6Act->setChecked(false);
		
		break;
	case 1:
		drawAxesJoint0Act->setEnabled(true);
		drawAxesJoint1Act->setEnabled(false);
		drawAxesJoint2Act->setEnabled(false);
		drawAxesJoint3Act->setEnabled(false);
		drawAxesJoint4Act->setEnabled(false);
		drawAxesJoint5Act->setEnabled(false);
		drawAxesJoint6Act->setEnabled(false);
		
		drawAxesJoint0Act->setChecked(true);
		drawAxesJoint1Act->setChecked(false);
		drawAxesJoint2Act->setChecked(false);
		drawAxesJoint3Act->setChecked(false);
		drawAxesJoint4Act->setChecked(false);
		drawAxesJoint5Act->setChecked(false);
		drawAxesJoint6Act->setChecked(false);
		
		break;
	case 2:
		drawAxesJoint0Act->setEnabled(true);
		drawAxesJoint1Act->setEnabled(true);
		drawAxesJoint2Act->setEnabled(false);
		drawAxesJoint3Act->setEnabled(false);
		drawAxesJoint4Act->setEnabled(false);
		drawAxesJoint5Act->setEnabled(false);
		drawAxesJoint6Act->setEnabled(false);
		
		drawAxesJoint0Act->setChecked(true);
		drawAxesJoint1Act->setChecked(true);
		drawAxesJoint2Act->setChecked(false);
		drawAxesJoint3Act->setChecked(false);
		drawAxesJoint4Act->setChecked(false);
		drawAxesJoint5Act->setChecked(false);
		drawAxesJoint6Act->setChecked(false);
		
		break;
	case 3:
		drawAxesJoint0Act->setEnabled(true);
		drawAxesJoint1Act->setEnabled(true);
		drawAxesJoint2Act->setEnabled(true);
		drawAxesJoint3Act->setEnabled(false);
		drawAxesJoint4Act->setEnabled(false);
		drawAxesJoint5Act->setEnabled(false);
		drawAxesJoint6Act->setEnabled(false);
		
		drawAxesJoint0Act->setChecked(true);
		drawAxesJoint1Act->setChecked(true);
		drawAxesJoint2Act->setChecked(true);
		drawAxesJoint3Act->setChecked(false);
		drawAxesJoint4Act->setChecked(false);
		drawAxesJoint5Act->setChecked(false);
		drawAxesJoint6Act->setChecked(false);
		break;
	case 4:
		drawAxesJoint0Act->setEnabled(true);
		drawAxesJoint1Act->setEnabled(true);
		drawAxesJoint2Act->setEnabled(true);
		drawAxesJoint3Act->setEnabled(true);
		drawAxesJoint4Act->setEnabled(false);
		drawAxesJoint5Act->setEnabled(false);
		drawAxesJoint6Act->setEnabled(false);
		
		drawAxesJoint0Act->setChecked(true);
		drawAxesJoint1Act->setChecked(true);
		drawAxesJoint2Act->setChecked(true);
		drawAxesJoint3Act->setChecked(true);
		drawAxesJoint4Act->setChecked(false);
		drawAxesJoint5Act->setChecked(false);
		drawAxesJoint6Act->setChecked(false);
		break;
	case 5:
		drawAxesJoint0Act->setEnabled(true);
		drawAxesJoint1Act->setEnabled(true);
		drawAxesJoint2Act->setEnabled(true);;
		drawAxesJoint3Act->setEnabled(true);
		drawAxesJoint4Act->setEnabled(true);
		drawAxesJoint5Act->setEnabled(false);
		drawAxesJoint6Act->setEnabled(false);
		
		drawAxesJoint0Act->setChecked(true);
		drawAxesJoint1Act->setChecked(true);
		drawAxesJoint2Act->setChecked(true);;
		drawAxesJoint3Act->setChecked(true);
		drawAxesJoint4Act->setChecked(true);
		drawAxesJoint5Act->setChecked(false);
		drawAxesJoint6Act->setChecked(false);
		break;
	case 6:
		drawAxesJoint0Act->setEnabled(true);
		drawAxesJoint1Act->setEnabled(true);
		drawAxesJoint2Act->setEnabled(true);
		drawAxesJoint3Act->setEnabled(true);
		drawAxesJoint4Act->setEnabled(true);
		drawAxesJoint5Act->setEnabled(true);
		drawAxesJoint6Act->setEnabled(false);
		
		drawAxesJoint0Act->setChecked(true);
		drawAxesJoint1Act->setChecked(true);
		drawAxesJoint2Act->setChecked(true);
		drawAxesJoint3Act->setChecked(true);
		drawAxesJoint4Act->setChecked(true);
		drawAxesJoint5Act->setChecked(true);
		drawAxesJoint6Act->setChecked(false);
		
		break;
	case 7:
		drawAxesJoint0Act->setEnabled(true);
		drawAxesJoint1Act->setEnabled(true);
		drawAxesJoint2Act->setEnabled(true);
		drawAxesJoint3Act->setEnabled(true);
		drawAxesJoint4Act->setEnabled(true);
		drawAxesJoint5Act->setEnabled(true);
		drawAxesJoint6Act->setEnabled(true);
			
		drawAxesJoint0Act->setChecked(true);
		drawAxesJoint1Act->setChecked(true);
		drawAxesJoint2Act->setChecked(true);
		drawAxesJoint3Act->setChecked(true);
		drawAxesJoint4Act->setChecked(true);
		drawAxesJoint5Act->setChecked(true);
		drawAxesJoint6Act->setChecked(true);
		break;
	default:
		break;
	}
}

void MainWindow::showNoAxes()
{
	drawAxesJoint0(false);
	drawAxesJoint1(false);
	drawAxesJoint2(false);
	drawAxesJoint3(false);
	drawAxesJoint4(false);
	drawAxesJoint5(false);
	drawAxesJoint6(false);
	
	drawAxesJoint0Act->setChecked(false);
	drawAxesJoint1Act->setChecked(false);
	drawAxesJoint2Act->setChecked(false);
	drawAxesJoint3Act->setChecked(false);
	drawAxesJoint4Act->setChecked(false);
	drawAxesJoint5Act->setChecked(false);
	drawAxesJoint6Act->setChecked(false);
}

void MainWindow::drawAxesJoint0(bool checked)
{
	if (robot->jointList.size() >= 1)
		robot->jointList[0].setDrawAxes(checked);
}

void MainWindow::drawAxesJoint1(bool checked)
{
	if (robot->jointList.size() >= 2)
		robot->jointList[1].setDrawAxes(checked);
}

void MainWindow::drawAxesJoint2(bool checked)
{
	if (robot->jointList.size() >= 3)
		robot->jointList[2].setDrawAxes(checked);
}

void MainWindow::drawAxesJoint3(bool checked)
{
	if (robot->jointList.size() >= 4)	
		robot->jointList[3].setDrawAxes(checked);
}

void MainWindow::drawAxesJoint4(bool checked)
{
	if (robot->jointList.size() >= 5)
		robot->jointList[4].setDrawAxes(checked);
}

void MainWindow::drawAxesJoint5(bool checked)
{
	if (robot->jointList.size() >= 6)
		robot->jointList[5].setDrawAxes(checked);
}

void MainWindow::drawAxesJoint6(bool checked)
{
	if (robot->jointList.size() == 7)
		robot->jointList[6].setDrawAxes(checked);
}

void MainWindow::about()
{
QMessageBox::about(this, tr("About UBC Robot Visualizer"),
             tr("This assists in the learning and understanding of robotics "
             "by displaying robots and allowing the user to mainpulate them. \n\n"
             "\tWritten by:\n\n"
             "\t\tZachary Job\n"
             "\t\tAntony Mbugua\n"
             "\t\tKevin Uribe"));
}
